//
//  LaunchViewController.m
//  MELive
//
//  Created by 曹曹 on 2016/10/19.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "LaunchViewController.h"
#import "LoginBacgroundPlayer.h"
#import "VenderLoginView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "LaunchData.h"
#import "LaunchRoot.h"
#import "LaunchSplash.h"
#import "YYModel.h"
#import "LoginViewController.h"

@interface LaunchViewController ()

// 第三方登录
@property (nonatomic, strong) VenderLoginView *venderView;
// 播放器
@property (nonatomic, strong) LoginBacgroundPlayer *player;
// 启动图
@property (nonatomic, strong) UIImageView *coverView;
// 网络图片
@property (nonatomic, strong) UIImageView *splashView;

@property (nonatomic, strong) LaunchRoot *root;

@end

@implementation LaunchViewController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_coverView removeFromSuperview];
    [_splashView removeFromSuperview];
    _splashView = nil;
    _coverView = nil;
    NSLog(@"dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notifi:)
                                                 name:AFNetworkingReachabilityDidChangeNotification
                                               object:nil];
    [self loadSplashAnimation];
}

#pragma mark - 欢迎动画
- (void)loadSplashAnimation {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 2.f;
    
    [manager POST:@"http://me.yy.com:80/app/splash"
       parameters:nil
         progress:^(NSProgress * _Nonnull uploadProgress) {
            
         } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             // 转数据模型
             _root = [LaunchRoot yy_modelWithJSON:responseObject];
             // 有值时加载网络图片
             if (_root.data.splashs.count > 0) {
                 _splashView = [[UIImageView alloc] initWithFrame:self.view.bounds];
                 [self.view addSubview:_splashView];
                 [self.view bringSubviewToFront:_splashView];
                 NSString *urlStr = _root.data.splashs[0].imgUrl;
                 [_splashView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
                 // 网络图片展示2秒后进入登录页面
                 [self performSelector:@selector(splashDidFinish) withObject:nil afterDelay:2];
             } else {
                 // 无值时播放欢迎动画
                 [self playSplashAnimation];
             }
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [self playSplashAnimation];
             NSLog(@"%@", error.description);
         }];
}

#pragma mark - 播放欢迎动画
- (void)playSplashAnimation {
    NSString *path = [NSString stringWithFormat:@"%d", arc4random() % 5];
    _player = [[LoginBacgroundPlayer alloc] initWithUrlForResource:path
                                                              type:@"mp4"
                                                             frame:self.view.bounds];
    [self.view.layer addSublayer:_player.playerLayer];
    // 欢迎动画结束后进入登录页面
    [self performSelector:@selector(splashDidFinish) withObject:nil afterDelay:3];
}

#pragma mark - 进入登录
- (void)splashDidFinish {
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    self.modalPresentationStyle = UIModalPresentationPopover;
    [self presentViewController:loginVC animated:YES completion:nil];
}

#pragma mark - 删除启动图和欢迎动画
- (void)deleteSplashView {
    [_coverView removeFromSuperview];
    [_splashView removeFromSuperview];
    _splashView = nil;
    _coverView = nil;
}

#pragma mark - 检测网络状态
- (void)notifi:(NSNotification *)noti{
    
    NSDictionary *dic = noti.userInfo;
    //获取网络状态
    NSInteger status = [[dic objectForKey:@"AFNetworkingReachabilityNotificationStatusItem"] integerValue];
    
    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:{
            [self playSplashAnimation];
            NSLog(@"无网络连接");
        }
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:{
            
            NSLog(@"移动蜂窝网络");
        }
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:{

            NSLog(@"wifi网络");
        }
            break;
        case AFNetworkReachabilityStatusUnknown:{
            
            NSLog(@"无法获取网络状态");
        }
            break;
    }
    NSLog(@"123");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
